package cz.khfotbal.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cz.khfotbal.dao.FrontMatchDao;
import cz.khfotbal.dao.TableDao;
import cz.khfotbal.domain.FrontMatch;
import cz.khfotbal.domain.FrontTable;

@Repository
@Transactional
public class FrontMatchDaoImpl implements FrontMatchDao {

	@Autowired
	private SessionFactory sessionFactory;

	private Session session;

	public void saveFrontMatches(FrontMatch frontMatch) {
		session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(frontMatch);
	}
	
	public List<FrontMatch> getListMatches(int scid, int page) {
		session = sessionFactory.getCurrentSession();
		List<FrontMatch> list = session.createQuery(
				"from FrontMatch where seasonCompetionId = " + scid + "and kolo = "+page).list();
		return list;
	}
	
	public void deleteAll(){
		session = sessionFactory.getCurrentSession();
		String hql = String.format("delete from FrontMatch");
	    Query query = session.createQuery(hql);
	    query.executeUpdate();
	}

//	public void deleteBysc(int sc){
//		session = sessionFactory.getCurrentSession();
//		String hql = String.format("delete from FrontTable where seasonCompetionId = "+sc);
//	    Query query = session.createQuery(hql);
//	    query.executeUpdate();
//	}
	
//	public void saveMatchDelegat(MatchDelegat md) {
//		session = sessionFactory.getCurrentSession();
//		session.saveOrUpdate(md);
//
//	}
//
//	public MatchDelegat getMatchDelegat(Match match, Delegat delegat) {
//		session = sessionFactory.getCurrentSession();
//		return (MatchDelegat) session.createQuery(
//				"from MatchDelegat md where md.match.id=" + match.getId()
//						+ " and md.delegat.id=" + delegat.getId())
//				.uniqueResult();
//	}
//
	
//
//	public void deleteReferees(Match match) {
//		session = sessionFactory.getCurrentSession();
//		// match.getMatchReferees().clear();
//		session.save(match);
//		// List<MatchReferee> rList = session.createQuery(
//		// "from MatchReferee mr where mr.match.id=" + match.getId())
//		// .list();
//		// for (MatchReferee mr : rList) {
//		// session.delete(mr);
//		// }
//
//	}
//
//	public void setReferee(Referee referee, Match match) {
//		session = sessionFactory.getCurrentSession();
//		MatchReferee mr = new MatchReferee();
//		mr.setReferee(referee);
//		mr.setMatch(match);
//		mr.setTyp(referee.getTyp());
//		session.save(mr);
//	}
//
//	public void deleteMatch(Match selectedMatch) {
//		session = sessionFactory.getCurrentSession();
//		session.delete(selectedMatch);
//
//	}
//
//	public List<Match> findMatchesBySeasonAndTeam(Season season, Team team) {
//		session = sessionFactory.getCurrentSession();
//		SeasonCompetionTeam sct = (SeasonCompetionTeam) session.createQuery(
//				"from SeasonCompetionTeam sct where sct.seasonCompetion.season.id="
//						+ season.getId() + " and sct.team.id=" + team.getId())
//				.uniqueResult();
//		List<SeasonCompetionTeamMatch> list = session.createQuery(
//				"from SeasonCompetionTeamMatch sctm where sc.seasonCompetionTeam.id"
//						+ sct.getId()).list();
//		return null;
//	}
//
//	public List<Match> findMatchesByDate(String date) {
//		session = sessionFactory.getCurrentSession();
//		List<Match> list = session.createQuery(
//				"from Match where datum = '" + date + "'").list();
//		return list;
//	}
	
}
